package com.itheima.studentmanager.controller;

import com.itheima.studentmanager.pojo.Student;
import com.itheima.studentmanager.service.StudentService;
import com.itheima.studentmanager.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * The type Student controller.
 */
@RestController
public class StudentController implements WebMvcConfigurer {
    //允许跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/users/**")  // 设置要允许跨域的接口路径
                .allowedOrigins("*")  // 设置允许访问的源地址
                .allowedMethods("GET", "POST", "PUT", "DELETE")  // 允许的请求方法
                .allowedHeaders("*");  // 允许的请求头
    }


    @Autowired
    private StudentService studentService;

    /**
     * Students result.
     *
     * @param student the student
     * @return the result
     */
    @PostMapping("/users/register")
    public Result students(@RequestBody Student student) {
        try {
            Student registerStu = studentService.register(student);
            return Result.success(registerStu);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * Gets student by id.
     *
     * @param id the id
     * @return the student by id
     */
    @GetMapping("/users/{id}")
    public Result getStudentById(@PathVariable Integer id) {

        try {
            Student student = studentService.getStudentById(id);
            return Result.success(student);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * Delete student by id result.
     *
     * @param id the id
     * @return the result
     */
    @DeleteMapping("/users/{id}")
    public Result deleteStudentById(@PathVariable Integer id) {
        studentService.deleteStudentById(id);
        return Result.success();
    }

    /**
     * Find all student result.
     *
     * @return the result
     */
    @GetMapping("/users/list")
    public Result findAllStudent() {
        List<Student> studentList = studentService.findAllStudent();
        return Result.success(studentList);
    }

    /**
     * Update student result.
     *
     * @param student the student
     * @return the result
     */
    @PutMapping("/users")
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * Delete batch student result.
     *
     * @param ids the ids
     * @return the result
     */
    @DeleteMapping("/users/delBatch")
    public Result deleteBatchStudent(@RequestBody List<Integer> ids) {
        studentService.deleteBatchStudent(ids);
        return Result.success();
    }


}
